package com.luzhikun.defineservlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;


/**
 * 1.请求参数是存放在Map中的
 * 2、这个Map的key为请求参数的名称，为String类型
 * 3、这个Map的value为请求参数的所有值，为String[]类型
 * 4、使用最多的是getParameter方法，它等价于 getParameterValues()[0]  \
 */

@WebServlet(name = "SomeServlet")
public class SomeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("开始执行");
        //获取指定名称的请求参数值
        String name = request.getParameter("name");
//        String   =req.getParameter("age");
        System.out.println(name);


        //获取所有请求参数的值
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String s = parameterNames.nextElement();
            String parameter = request.getParameter(s);
            System.out.println(s + "===" + parameter);
        }

        //获取指定参数的所有值
        String[] hobbies = request.getParameterValues("hobby");
        //遍历
        for (String s : hobbies) {
            System.out.println(s);
        }


        //获取存放请求参数的Map
        Map<String, String[]> parameterMap = request.getParameterMap();
        //遍历Map
        for (String key : parameterMap.keySet()) {
            System.out.println(key + "---------------" + request.getParameter(key));
        }


    }
}
